
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "RTTree.h"
#include "RTDatabase.h"

//node class
#include "RTNode.h"
#include "RTNodeTranslate.h"
#include "RTNodeSeparator.h"
#include "RTNodeScale.h"
#include "RTNodeRotate.h"
#include "RTNodePrimitive.h"
#include "RTNodeMaterial.h"
#include "RTNodeLookAt.h"
#include "RTNodeDirectionalLight.h"
#include "RTNodePointLight.h"
using namespace RT;

namespace RT
{

	//set the static instance variable
	RTTree* RTTree::_instance = 0;

	RTTree& RTTree::getInstance()
	{
		if(_instance == 0)
			_instance = new RTTree();

		return *_instance;
	}

	void RTTree::close()
	{
		if(_instance != 0)
		{
			delete _instance;
			_instance = 0;
		}
	}

	RTTree::RTTree(void)
	:_root(0)
	{
		_value.identity();
	}

	RTTree::~RTTree(void)
	{
		//delete the tree
		/*if(_root != 0)
		{
			delete _root;
			_root = 0;
		}*/

		for(list<RTNode*>::iterator i = _nodesList.begin(); i != _nodesList.end(); i++)
		{
			if( *i != 0)
			{
				delete *i;
				*i = 0;
			}
		}

		_nodesList.clear();
	}

	void RTTree::setValue(const RTMatrix44& value)
	{
		_value = value;
	}

	RTMatrix44& RTTree::getValue()
	{
		return _value;
	}

	//get from pile
	void RTTree::pop()
	{
		if (_stack.empty())
		{
			_value.identity();
			return;
		}

		_value = _stack.top();
		_stack.pop();
	}

	//put on pile
	void RTTree::push()
	{
		_stack.push(_value);
	}

	void RTTree::dirty()
	{
		//dirty the root
		_root->dirty();

		//clean the stack matrix
		while(!_stack.empty())
			_stack.pop();

		//clean the current matrix
		_value.identity();
	}

	void RTTree::traversal()
	{
		//dirty all the controllers
		//CTRLDATA.onChange();

		//traverse the tree and update everything
		_root->goThrough();
	}

	void RTTree::setRoot(RTNode* root)
	{
		_root = root;
	}

	//create a translate node and return a poI32er to it
	RTNodeTranslate* RTTree::createNodeTranslate()
	{
		RTNodeTranslate* newNode = new RTNodeTranslate();
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a separator node and return its poI32er
	RTNodeSeparator* RTTree::createNodeSeparator()
	{
		RTNodeSeparator* newNode = new RTNodeSeparator();
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a scale node and return its poI32er
	RTNodeScale* RTTree::createNodeScale()
	{
		RTNodeScale* newNode = new RTNodeScale();
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a rotate node and return its poI32er
	RTNodeRotate* RTTree::createNodeRotate()
	{
		RTNodeRotate* newNode = new RTNodeRotate();
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a RTPrimitive node and return its poI32er
	RTNodePrimitive* RTTree::createNodePrimitive(RTIPrimitive* p)
	{
		RTNodePrimitive* newNode = new RTNodePrimitive(p);
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a meterial node and return its poI32er
	RTNodeMaterial* RTTree::createNodeMaterial(RTMaterial* m)
	{
		RTNodeMaterial* newNode = new RTNodeMaterial(m);
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a lookat node and return its poI32er
	RTNodeLookAt* RTTree::createNodeLookAt()
	{
		RTNodeLookAt* newNode = new RTNodeLookAt();
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a directional light node and return its poI32er
	RTNodeDirectionalLight* RTTree::createNodeDirectionalLight(RTDirectionalLight* l)
	{
		RTNodeDirectionalLight* newNode = new RTNodeDirectionalLight(l);
		_nodesList.push_back(newNode);
		return newNode;
	}

	//create a poI32 light node and return its poI32er
	RTNodePointLight* RTTree::createNodePointLight(RTPointLight* l)
	{
		RTNodePointLight* newNode = new RTNodePointLight(l);
		_nodesList.push_back(newNode);
		return newNode;
	}

}